package com.itheima.service.impl;

import com.itheima.mapper.AssignmentMapper;
import com.itheima.pojo.Assignment;
import com.itheima.pojo.AssignmentFinishTimes;
import com.itheima.pojo.UserAssignment;
import com.itheima.service.AssignmentService;
import com.itheima.utils.ResponseResult;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AssignmentServiceImpl implements AssignmentService {

    @Autowired
    private AssignmentMapper assignmentMapper;

    @Override
    public ResponseResult addAssignment(Assignment assignment) {

        if(assignmentMapper.addAssignment(assignment)>0){
            return new ResponseResult(0,"操作成功","作业发布成功");
        }

        return new ResponseResult(1,"操作失败","作业发布失败");
    }

    @Override
    public ResponseResult findStudentAssignmentByTimeAndType(Date time, Long type) {
        List<UserAssignment> studentAssignments= assignmentMapper.findStudentAssignmentByTimeAndType(time, type);
        if(studentAssignments!=null){
            return new ResponseResult(0,"操作成功",studentAssignments);
        }

        return new ResponseResult(1,"操作失败");
    }

    @Override
    public ResponseResult findStudentByClassIdAndTypeAndTime(Map<String,String> map) throws ParseException {
//        Long classId, Integer type, Date startTime, Date endTime
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        List<AssignmentFinishTimes> finishList = assignmentMapper.findStudentFinishTimesByClassIdAndTypeAndTime(Long.parseLong(map.get("classId")),
                Integer.parseInt(map.get("type")),
                sFormat.parse(map.get("startTime")),
                sFormat.parse(map.get("endTime")));

        if (finishList!= null){
            return new ResponseResult(0,"操作成功",finishList);
        }

        return new ResponseResult(1,"操作失败");

    }


}
